package com.LeeCode;

import java.util.HashSet;
import java.util.Set;

/**
 * 定长子串中元音的最大数目
 */

public class Code1456 {
    public static void main(String[] args) {
        String s = "zpuerktejfp";
        int k = 3;
        System.out.println(new Code1456().maxVowels(s, k));

    }

    public int maxVowels(String s, int k) {
        int ans = 0, count = ans;
        Set<Character> vowels = new HashSet<>();
        vowels.add('a');
        vowels.add('e');
        vowels.add('i');
        vowels.add('o');
        vowels.add('u');

        for (int i = 0; i < k; i++) {
            if (vowels.contains(s.charAt(i))) {
                count++;
            }
        }
        ans = Math.max(ans, count);
        if (ans == k)
            return ans;

        for (int i = k; i < s.length(); i++) {
            if(vowels.contains(s.charAt(i-k))){
                count--;
            }
            if(vowels.contains(s.charAt(i))){
                count++;
            }
            ans = Math.max(ans, count);
            if(ans == k)
                return ans;
        }

        return ans;
    }
}
